package cn.js.sangu.unitStore.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 说明：跨域配置
 * 作者：linshuo
 * 日期：2024/8/8 12:21
 */
@Configuration
public class CorsConfig {

    private static final long MAX_AGE = 60 * 60 * 24;

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.addAllowedOriginPattern("*");// 允许所有源地址
        // 访问源地址（前端特定源）
        configuration.setAllowCredentials(true);// 允许携带凭证
        configuration.addAllowedHeader("*");// 访问源请求头
        configuration.addAllowedMethod("*");// 访问源请求方法
        configuration.setMaxAge(MAX_AGE);
        source.registerCorsConfiguration("/**", configuration);// 将跨域配置设置到对象
        return new CorsFilter(source);
    }

}
